package com.jin.lazytool.entity;


import com.jin.lazytool.utils.JinUtils;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import org.luaj.vm2.LuaTable;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;

public class InputStringEvent extends IEvent {
    public InputStringEvent() {
    }

    private String text;

    @Override
    public int execute(Robot robot,int index) throws CharacterCodingException,InterruptedException,NoSuchMethodException {
        if(luaFLag){
            LuaTable lua = JinUtils.evalLua(getLua(), getTime());
            boolean exeFlag = lua.get("exeFlag").toboolean();
            if(exeFlag){
                Thread.sleep(lua.get("time").tolong());
                String text = lua.get("text").toString();
                robot.delay(500);
                keyPressWithAltCode(robot,text,5);
            }
        }else{
            Thread.sleep(getTime());
            robot.delay(500);
            keyPressWithAltCode(robot,text,5);
        }
        return index;
    }
    /**
     * 使用 Robot 利用键盘 alt 码输出字符
     * @param r      Robot 对象
     * @param gbkCode 待打印的字符的国标区位码
     * @param time   每个字符等待的时间
     */
    public static void keyPressWithAltCode(Robot r, int gbkCode, int time){
        r.keyPress(KeyEvent.VK_ALT);
        String s = Integer.toString(gbkCode);
        for (char c : s.toCharArray()) {
            // 由 ASCII 码 0(48) 转为  VK_NUMPAD0(0x60)
            int k = c + 48;
            r.keyPress(k);
            r.keyRelease(k);
        }
        r.keyRelease(KeyEvent.VK_ALT);
        if (time > 0) {
            r.delay(time);
        }
    }

    /**
     * 使用 Robot 利用键盘 alt 码输出字符
     * @param r      Robot 对象
     * @param string 待打印的字符
     * @param time   每个字符等待的时间
     */
    public static void keyPressWithAltCode(Robot r, String string, int time) throws CharacterCodingException {
        CharsetEncoder ce = Charset.forName("GBK").newEncoder();
        for (char c : string.toCharArray()) {
            if (Character.isSurrogate(c)) {
                // 跳过补充字符
                continue;
            }
            if (c >>> 8 == 0) {
                keyPressWithAltCode(r, c, time);
                continue;
            }
            CharBuffer cb = CharBuffer.wrap(new char[]{c});
            final byte[] array = ce.encode(cb).array();
            assert array.length == 2;
            int code = Byte.toUnsignedInt(array[0]) << 8 | Byte.toUnsignedInt(array[1]);
            keyPressWithAltCode(r, code, time);
        }
    }
    @Override
    public Pane initDesc() {
        FlowPane pane = new FlowPane();
        pane.setStyle("-fx-pref-height: 26;");
        Label label = new Label("输入字符串: ");
        label.setTextFill(Color.GRAY);
        TextField field = new TextField();
        field.editableProperty().set(false);
        field.setText(text);
        pane.getChildren().addAll(label, field);
        return pane;
    }


    @Override
    public void show() {

    }
    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

}
